# 评估共享单车的需求分布
import csv
import numpy as np
filename = "F:/python学习资料/Python-Machine-Learning-Cookbook-master/Chapter01/bike_day.csv"
file_reader = csv.reader(open(filename,'r'),delimiter=',')
X, y = [], []
for row in file_reader:
    X.append(row[2:13])
    y.append(row[-1])

# 提取特征名称
feature_names = np.array(X[0])
print("特征名称",feature_names)

# 把两个列表的数据转化为整型数组
X = np.array(X[1:]).astype(np.float32)
y = np.array((y[1:])).astype(np.float32)

from sklearn.utils import shuffle
X,y = shuffle(X,y,random_state=7)
num_training = int(0.9*len(X))
X_train,y_train = X[:num_training],y[:num_training]
X_test,y_test = X[num_training:],y[num_training:]

from sklearn.ensemble import RandomForestRegressor
rf_regressor = RandomForestRegressor(n_estimators=1000,max_depth=10,min_samples_split=2)
rf_regressor.fit(X_train,y_train)

# 评估随机森林的性能
y_pred = rf_regressor.predict(X_test)
from sklearn.metrics import mean_squared_error,explained_variance_score
mse = mean_squared_error(y_test,y_pred)
evs = explained_variance_score(y_test,y_pred)
print("### Random Forest regressor performance ###")
print("Mean squared error:",round(mse,2))
print("Explained variance score:",round(evs,2))

# 提取特征的相对重要性
RFFImp = rf_regressor.feature_importances_
RFEImp = 100*(RFFImp/max(RFFImp))
index_sorted = np.flipud(np.argsort(RFFImp))
pos = np.arange(index_sorted.shape[0])+0.5
# 对结果可视化
print(pos)
import matplotlib.pyplot as plt
plt.figure()
plt.bar(pos,RFFImp[index_sorted],align='center')
plt.xticks(pos,feature_names[index_sorted])
plt.ylabel("Relative Importance")
plt.title("Random Forest regressor")
plt.show()